package juc.atomic;

import com.sun.jdi.Value;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class AtomicIntegerTest {
    /**
     * 1. 可见性
     * 2. 顺序性
     * 3. 不能保证原子性
     */
    private static volatile int value;
    private static Set<Integer> set = Collections.synchronizedSet(new HashSet<Integer>());

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread() {
            @Override
            public void run() {
                int x = 0;
                while (x < 500) {
                    set.add(value);
                    int tmp = value;
                    System.out.println(Thread.currentThread().getName() + ": " + tmp);
                    /**
                     * 一个线程执行这4个步骤中的某一步时，有可能有其他线程操作
                     * value=value+1;
                     * (1) get value from main memory to local memory
                     * (2) add 1=>x
                     * (3) assign the value to x
                     * (4) flush to main memory
                     */
                    value += 1;
                    x++;
                }
            }
        };
        Thread t2 = new Thread() {
            @Override
            public void run() {
                int x = 0;
                while (x < 500) {
                    set.add(value);
                    int tmp = value;
                    System.out.println(Thread.currentThread().getName() + ": " + tmp);
                    value += 1;
                    x++;
                }
            }
        };
        Thread t3 = new Thread() {
            @Override
            public void run() {
                int x = 0;
                while (x < 500) {
                    set.add(value);
                    int tmp = value;
                    System.out.println(Thread.currentThread().getName() + ": " + tmp);
                    value += 1;
                    x++;
                }
            }
        };
        t1.start();
        t2.start();
        t3.start();
        t1.join();
        t2.join();
        t3.join();
        System.out.println(set.size());
    }
}
